package com.asyyy.shixun.type;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.asyyy.shixun.R;
import com.asyyy.shixun.base.BaseFragment;
import com.asyyy.shixun.home.Goods;
import com.asyyy.shixun.home.GoodsListAdapter;

import java.util.ArrayList;

public class Jdtuijian extends BaseFragment {

    private GoodsListAdapter goodsListAdapter,goodsListAdapter2,goodsListAdapter3,
            goodsListAdapter4,goodsListAdapter5,goodsListAdapter6;
    private ArrayList<Goods> goodsList,goodsList2,goodsList3,goodsList4,
            goodsList5,goodsList6;
    private ListView type_list;
    private GridView gridview_jd_r1;
    private TextView tv_jd_commr1;
    private String[] imagenames1 = {"充电宝","空调","口红","平板电脑","眼影", "手机"};
    private int[] ids1 = {R.drawable.a105,R.drawable.a104,R.drawable.a103,
            R.drawable.a102,R.drawable.a101,R.drawable.a100};

    @Override
    public View initView() {
        //初始化界面
        View view = View.inflate(mContext, R.layout.type_grid,null);
        gridview_jd_r1 = view.findViewById(R.id.gridview_jd_r1);
        tv_jd_commr1 = view.findViewById(R.id.tv_jd_commr1);
        type_list = view.findViewById(R.id.type_list);
        gridview_jd_r1.setAdapter(new HomeAdapter1());

        GridListener();

        return view;
    }
    public void initData(){
        super.initData();
        goodsList = new ArrayList<>();
        //int g_id, int g_photo, String g_name, String g_type, double g_price, int g_sales, String g_shop

        goodsList.add(new Goods(9,R.drawable.c101,"小米移动电源3","10000mAh 快充版",79.00,2800,"小米京东自营旗舰店"));
        goodsList.add(new Goods(10,R.drawable.c102,"小米移动电源3","20000mAh USB—C双向快充版",129.00,21000,"小米京东自营旗舰店"));
        goodsList.add(new Goods(11,R.drawable.c103,"罗马仕LT20","20000mAh",79.00,3000,"罗马仕京东自营旗舰店"));

        goodsList2 = new ArrayList<>();
        goodsList2.add(new Goods(12,R.drawable.c201,"格力","1.5匹 一级能效",2899.00,14000,"格力京东自营旗舰店"));
        goodsList2.add(new Goods(13,R.drawable.c202,"美的","1.5匹 变频智弧",2099.00,18000,"美的京东自营旗舰店"));
        goodsList2.add(new Goods(14,R.drawable.c203,"海尔","1.5匹 变频自清洁",1899.00,9000,"海尔京东自营旗舰店"));

        goodsList3 = new ArrayList<>();
        goodsList3.add(new Goods(15,R.drawable.c301,"迪奥","哑光999",330.00,390000,"迪奥美妆京东专营店"));
        goodsList3.add(new Goods(16,R.drawable.c302,"纪梵希","小羊皮306",338.00,18000,"纪梵希美妆京东专营店"));
        goodsList3.add(new Goods(17,R.drawable.c303,"圣罗兰","复古哑光",335.00,10000,"圣罗兰美妆京东专营店"));

        goodsList4 = new ArrayList<>();
        goodsList4.add(new Goods(18,R.drawable.c101,"华为平板","麒麟810 10.4英寸",2199.00,30000,"华为京东自营旗舰店"));
        goodsList4.add(new Goods(19,R.drawable.c102,"荣耀平板v6","麒麟985 10.4英寸",2498.00,21000,"荣耀京东自营旗舰店"));
        goodsList4.add(new Goods(20,R.drawable.c103,"Apple iPad","2019款 10.2英寸",2869.00,31000,"Apple京东自营旗舰店"));

        goodsList5 = new ArrayList<>();
        goodsList5.add(new Goods(21,R.drawable.c501,"完美日记","十二色 粉质细腻",119.00,5300,"完美日记京东自营旗舰店"));

        goodsList6 = new ArrayList<>();
        goodsList6.add(new Goods(22,R.drawable.ipad,"Ipad 2018金","128G 金色",2999.00,9,"Apple产品京东自营旗舰店"));
        goodsList6.add(new Goods(23,R.drawable.ipad2,"Ipad 2018银","32G 银色",2499.00,99,"Apple产品京东自营旗舰店"));
        goodsList6.add(new Goods(24,R.drawable.ipad3,"Ipad Pro","11英寸",6599.00,5,"Apple产品京东自营旗舰店"));
        goodsList6.add(new Goods(25,R.drawable.xiaomi10,"小米（MI）10","蓝色 8+256",3799.00,19,"小米京东自营旗舰店"));
        goodsList6.add(new Goods(26,R.drawable.huaweip40,"华为P40 5G手机","白色 12+512",4488.00,18,"华为京东自营旗舰店"));

    }
    //显示gridview内容
    public class HomeAdapter1 extends BaseAdapter {
        @Override
        public int getCount() {
            return imagenames1.length;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            tv_jd_commr1.setText("常用分类");
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.type_grid_item1, null);
            ImageView iv_icon = (ImageView) view.findViewById(R.id.ic_icon);
            TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_name.setText(imagenames1[position]);
            iv_icon.setImageResource(ids1[position]);
            return view;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
    }
    //监听GridView点击事件
    private void GridListener(){
        gridview_jd_r1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int index = position + 1;// id是从0开始的，所以需要+1
                switch (index){
                    case 1:
                        //实例化适配器
                        goodsListAdapter = new GoodsListAdapter(mContext,goodsList);
                        type_list.setAdapter(goodsListAdapter);
                        type_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                int indexs = position + 1;
                                if (indexs == 1){
                                   Intent intent;
                                    intent = new Intent(mContext,ChatActivity.class);
                                    startActivity(intent);
                                }
                            }
                        });
                        break;
                    case 2:
                        goodsListAdapter2 = new GoodsListAdapter(mContext,goodsList2);
                        type_list.setAdapter(goodsListAdapter2);
                        break;
                    case 3:
                        goodsListAdapter3 = new GoodsListAdapter(mContext,goodsList3);
                        type_list.setAdapter(goodsListAdapter3);
                        break;
                    case 4:
                        goodsListAdapter4 = new GoodsListAdapter(mContext,goodsList4);
                        type_list.setAdapter(goodsListAdapter4);
                        break;
                    case 5:
                        goodsListAdapter5 = new GoodsListAdapter(mContext,goodsList5);
                        type_list.setAdapter(goodsListAdapter5);
                        break;
                    case 6:
                        goodsListAdapter6 = new GoodsListAdapter(mContext,goodsList6);
                        type_list.setAdapter(goodsListAdapter6);
                        break;
                        default:
                            break;
                }
                //Toast.makeText(getActivity(), "你按下了选项：" + index, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
